package edu.columbia.cloudbox.ui;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import edu.columbia.cloudbox.R;
import edu.columbia.cloudbox.UserStatusService;
import edu.columbia.cloudbox.io.FileType;
import edu.columbia.cloudbox.io.S3IO;
import edu.columbia.cloudbox.policy.CBDBHelper;
import edu.columbia.cloudbox.io.S3ObjectNotFoundException;

public class AllFilesAdapter extends ArrayAdapter<FileType> {

	private final static String LOG_TAG = "AllFilesAdapter";

	private final Context context;
	private final List<FileType> files;

	public AllFilesAdapter(Context context, List<FileType> files) {
		super(context, R.layout.all_file_row, files);
		this.context = context;
		this.files = files;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		LayoutInflater inflator = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		// Get the row of list item
		View rowView = inflator.inflate(R.layout.all_file_row, parent, false);

		// Current item(file) in the list
		final FileType file = files.get(position);

		// Set name of file
		TextView textView = (TextView) rowView.findViewById(R.id.allfilename);
		final String fileName = file.getName();
		textView.setText(fileName);

		// Set image
		ImageView imageView = (ImageView) rowView
				.findViewById(R.id.allfileicon);

		final boolean isFolder = files.get(position).isFolder();
		// String type = files.get(position).getType();

		if (isFolder) {
			imageView.setImageResource(R.drawable.icon_folder);
		} else {
			imageView.setImageResource(R.drawable.icon_doc);
		}

		// Set tag<isFolder> in the row
		rowView.setTag(isFolder);

		/** Set onClick Listener -- Save file/dir from S3 to local **/
		Button saveBtn = (Button) rowView.findViewById(R.id.btn_save);
		saveBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.i(LOG_TAG, "Button Clicked");
				// Make path for downloading file
				String path = FilePathOnTab
						.makePath(FilePathOnTab.currentPath_allFiles);
				boolean result = false;
				if (!isFolder) {
					result = download(UserStatusService.USER_NAME, path,fileName);
					CBDBHelper dbHelper = new CBDBHelper(context);
					dbHelper.insertFile(file);
					dbHelper.close();
				} else {
					result = downloadFolder(UserStatusService.USER_NAME, path,fileName);
					CBDBHelper dbHelper = new CBDBHelper(context);
					List<FileType> files = getAllFilesByFolder(UserStatusService.USER_NAME, path,fileName);
					dbHelper.insertFolder(files);
					dbHelper.close();
				}
				Log.i(LOG_TAG, "Download " + fileName + " result: " + result);
			}
		});
		return rowView;
	}
	

	/**
	 * Helper method - download file from S3
	 * 
	 * @param username
	 * @param path
	 * @param fileName
	 * @return
	 */
	private boolean download(String userName, String path, String fileName) {
		S3IO s3io = UserStatusService.S3IO_INSTANCE;
		boolean result = false;
		try {
			if ("".equals(path)) {
				Log.i(LOG_TAG, "Key: " + userName + "/" + fileName);
				result = s3io.download(userName + "/" + fileName);
			} else {
				Log.i(LOG_TAG, "Key: " + userName + path + "/" + fileName);
				result = s3io.download(userName + path + "/" + fileName);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (S3ObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * Download a folder from S3
	 * 
	 * @param username
	 * @param path
	 * @param folderName
	 * @return
	 */
	private boolean downloadFolder(String userName, String path,
			String folderName) {
		S3IO s3io = UserStatusService.S3IO_INSTANCE;
		boolean result = false;
		Log.i(LOG_TAG, "Key: " + userName + "/" + folderName);
		try {
			if ("".equals(path))
				result = s3io.downloadFolder(userName + "/" + folderName);
			else
				result = s3io.downloadFolder(userName + path + "/" + folderName);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (S3ObjectNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * Browse all files from a folder from S3
	 * 
	 * @param username
	 * @param path
	 * @param folderName
	 * @return
	 */
	private List<FileType> getAllFilesByFolder(String userName, String path,
			String folderName) {
		S3IO s3io = UserStatusService.S3IO_INSTANCE;
		List<FileType> files = null;
		Log.i(LOG_TAG, "Key: " + userName + "/" + folderName);
		if ("".equals(path))
			files = s3io.getAllFileTypesByFolder(userName + "/" + folderName);
		else
			files = s3io.getAllFileTypesByFolder(userName + path + "/" + folderName);
		return files;
	}
}
